package com.yousoft.cfapi.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yousoft.cfapi.dao.ZandetailMapper;
import com.yousoft.cfapi.entity.Zandetail;
import com.yousoft.cfapi.entity.ZandetailExample;
import com.yousoft.cfapi.entity.ZandetailExample.Criteria;
import com.yousoft.cfapi.service.ZanService;

@Service
public class ZanServiceImpl implements ZanService {

	@Autowired
	private ZandetailMapper zanDetailMapper;

	@Override
	public List<Map<String, String>> findUserIdListByTextId(String textid,
			String userid) {
		List<String> nameList = zanDetailMapper.findZanListByTextId(
				Integer.valueOf(textid), userid);
		Map<String, String> map = null;
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		for (String name : nameList) {
			map = new HashMap<String, String>();
			map.put("nick", name);
			list.add(map);
		}
		return list;
	}

	@Override
	public void addZan(String textid, String userid) {
		ZandetailExample example = new ZandetailExample();
		Criteria criteria = example.createCriteria();
		criteria.andTextidEqualTo(BigDecimal.valueOf(Long.valueOf(textid)));
		criteria.andUseridEqualTo(userid);
		List<Zandetail> detailList = zanDetailMapper.selectByExample(example);
		if (detailList == null || detailList.size() < 1) {
			Zandetail zanDetail = new Zandetail();
			zanDetail.setTextid(BigDecimal.valueOf(Long.valueOf(textid)));
			zanDetail.setUserid(userid);
			zanDetailMapper.insertSelective(zanDetail);
		}
	}

	@Override
	public void cancleZan(String textid, String userid) {
		ZandetailExample example = new ZandetailExample();
		Criteria criteria = example.createCriteria();
		criteria.andTextidEqualTo(BigDecimal.valueOf(Long.valueOf(textid)));
		criteria.andUseridEqualTo(userid);
		zanDetailMapper.deleteByExample(example);
	}
}
